/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.gnucash.server;

import app.gnucash.client.thesisService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import db.myibat.data._SqlMapConfigReader;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author cc
 */
public class thesisServiceImpl extends RemoteServiceServlet implements thesisService {

//    //private Folder thesis;
//    private List<thesisItem> inboxItems;
//    private List<thesisItem> sentItems;
//    private List<thesisItem> trashItems;
    private final static SqlSessionFactory sqlFactory = _SqlMapConfigReader.getSqlSessionFactory();

    public thesisServiceImpl() {

//        Folder inbox = new Folder("Inbox");
//        Folder minbox = new Folder("MyInbox");
//        inbox.add(minbox);
//        Folder sent = new Folder("Sent Items");
//        Folder trash = new Folder("Trash");
//        List<thesisItem> items = TestData.getthesisItems();
//        int count = items.size();
//        inboxItems = new ArrayList<thesisItem>();
//        sentItems = new ArrayList<thesisItem>();
//        trashItems = new ArrayList<thesisItem>();
//        for (int i = 0;
//                i < count;
//                i++) {
//            thesisItem item = (thesisItem) items.get(i);
//            if (i < (count / 2)) {
//                inboxItems.add(item);
//            } else {
//                sentItems.add(item);
//            }
//        }
    }

     public String login(String username, String password) {
        String retStr = "";
        //SqlSession sqlsession = sqlFactory.openSession(true);
//        UserMapper UserMap = sqlsession.getMapper(UserMapper.class);
//        User u = UserMap.selectUser(username);

        if ((username != null) && (password.equals("cc123"))) {
            getThreadLocalRequest().getSession().setAttribute("user", username);
            getThreadLocalRequest().getSession().setAttribute("userId", 1);
            retStr = username;
            //Folder thesis = getthesisTitles(username);
            //getThreadLocalRequest().getSession().setAttribute(username, thesis);
        }
        //sqlsession.close();
        return retStr;
    }

//    public Boolean changePassword(String oldPassword, String newPassword) {
//        String userName = getSessionUser();
//        if (userName.equals("")) {
//            return false;
//        }
//        Boolean chpwdSuccess = false;
//        SqlSession sqlsession = sqlFactory.openSession(true);
//        UserMapper UserMap = sqlsession.getMapper(UserMapper.class);
//        User u = UserMap.selectUser(userName);
//        if ((u != null) && (oldPassword.equals(u.getPassword()))) {
////            User uUser = new User();
////            uUser.setId(u.getId());
////            uUser.setUsername(userName);
//            u.setPassword(newPassword);
//            try {
//                UserMap.updatePassword(u);
//                getThreadLocalRequest().getSession().removeAttribute("user");
//                chpwdSuccess = true;
//            } catch (Exception e) {
//                chpwdSuccess = false;
//            } finally {
//                if (sqlsession != null) {
//                    try {
//                        sqlsession.close();
//                    } catch (Exception e) {
//                    }
//                }
//            }
//        }
//        return chpwdSuccess;
//    }

    public String getSessionUser() {
        // get the Session information.
        String str = "";
        /**
         * If the Session Value "user" exists return it
         * Else return a empty String
         */
        if (getThreadLocalRequest().getSession().getAttribute("user") != null) {
            str = (String) getThreadLocalRequest().getSession().getAttribute("user");
        }
        return str;
    }
}
